package com.yf.exam.modules.trainingarchives.file.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.base.Preconditions;
import com.yf.exam.common.entity.Result;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.trainingarchives.file.dto.FileDTO;
import com.yf.exam.modules.trainingarchives.file.service.IFileService;
import com.yf.exam.modules.trainingarchives.file.vo.FileListVO;
import com.yf.exam.modules.trainingarchives.teacherattendance.dto.TeacherAttendanceDTO;
import com.yf.exam.modules.trainingarchives.teacherattendance.vo.TeacherAttendanceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 文件信息表 前端控制器
 * </p>
 *
 * @author lql
 * @since 2025-01-16
 */
@Api(tags={"文件上传V2"})
@RestController
@RequestMapping("/org/api/file")
@Slf4j
public class FileController {

    @Resource
    IFileService fileService;

    /**
     * 文件上传
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传", notes = "此接口较为特殊，参数都通过表单方式提交，而非JSON")
    public Result<Boolean> upload(String trainingClassInfoId, String fileType, @RequestParam(required = true) MultipartFile file) {
        // 上传并返回URL
        FileDTO reqDTO = new FileDTO();
        reqDTO.setFile(file);
        reqDTO.setFileType(fileType);
        reqDTO.setTrainingClassInfoId(trainingClassInfoId);
        fileService.upload(reqDTO);
        return Result.ok("文件上传成功！");
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody BaseIdReqDTO baseIdReqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeacherAttendanceController.delete.baseIdReqDTO:{}", JSON.toJSONString(baseIdReqDTO));
            }
            Preconditions.checkNotNull(baseIdReqDTO.getId(), "文件id不能为空!");
            Boolean result = fileService.removeById(baseIdReqDTO.getId());
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TeacherAttendanceController.delete.error:{}", e.getMessage(), e);
            return Result.faild("删除文件执行失败!");
        }
    }

    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public Result<IPage<FileListVO>> paging(@RequestBody PagingReqDTO<FileListVO> reqDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TeachingPlanExecutionController.paging.dto:{}", JSON.toJSONString(reqDTO));
            }
            //分页查询并转换
            IPage<FileListVO> page = fileService.paging(reqDTO);
            return Result.ok(page);
        } catch (Exception e) {
            log.error("TeachingPlanExecutionController.paging.error:{}", e.getMessage(), e);
            return Result.faild("查询失败");
        }
    }

}
